package com.open.capacity.salary.service.impl;

import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.exception.BusinessException;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.SysUserUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.salary.entity.GzZt;
import com.open.capacity.salary.dao.GzZtDao;
import com.open.capacity.salary.service.GzZtService;


@Service
public class GzZtServiceImpl implements GzZtService {

    @Autowired
    private GzZtDao gzZtDao;

    /**
     * 添加
     *
     * @param gzZt
     */
    public int save(GzZt gzZt) {
        SysUser loginUser = SysUserUtil.getLoginAppUser();
        GzZt existsCode = gzZtDao.findByCode(gzZt.getCompanyId(), gzZt.getCode());
        if (existsCode != null) {
            throw new BusinessException("账套编号冲突");
        }
        GzZt existsName = gzZtDao.findByName(gzZt.getCompanyId(), gzZt.getName());
        if (existsName != null) {
            throw new BusinessException("账套名称冲突");
        }
        gzZt.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        gzZt.setStatus(1);
        gzZt.setUpdateTime(new Date());
        gzZt.setUpdateUserid(loginUser.getId());
        gzZt.setUpdateUsername(loginUser.getRealName());
        return gzZtDao.save(gzZt);
    }

    /**
     * 修改
     *
     * @param gzZt
     */
    public int update(GzZt gzZt) {
        SysUser loginUser = SysUserUtil.getLoginAppUser();
        GzZt exists = gzZtDao.findByName(gzZt.getCompanyId(), gzZt.getName());
        if (exists != null && !exists.getId().equals(gzZt.getId())) {
            throw new BusinessException("账套名称冲突");
        }
        gzZt.setUpdateTime(new Date());
        gzZt.setUpdateUserid(loginUser.getId());
        gzZt.setUpdateUsername(loginUser.getRealName());
        return gzZtDao.update(gzZt);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return gzZtDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public GzZt findById(String id) {
        return gzZtDao.findById(id);
    }

    /**
     * 查询记录
     *
     * @param ids
     * @return
     */
    public String findNameByIds(String ids) {
        String ztName = "";
        List<GzZt> ztList = gzZtDao.findByIds(Arrays.asList(ids.split(",")));
        if (CollectionUtils.isNotEmpty(ztList)) {
            for (GzZt zt : ztList) {
                if (StringUtils.isEmpty(ztName)) {
                    ztName += zt.getName();
                } else {
                    ztName += "、" + zt.getName();
                }
            }
        }
        return ztName;
    }

    /**
     * 查询记录
     *
     * @return
     */
    public GzZt findByCode(String companyId, String code) {
        return gzZtDao.findByCode(companyId, code);
    }


    /**
     * 查询记录
     *
     * @return
     */
    public GzZt findByName(String companyId, String name) {
        return gzZtDao.findByName(companyId, name);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<GzZt> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<GzZt> list = gzZtDao.findAll(params);
        PageInfo<GzZt> pageInfo = new PageInfo(list);

        return PageResult.<GzZt>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<GzZt> findAll(Map<String, Object> params) {
        return gzZtDao.findAll(params);
    }

}
